$(function(){

//生成页面
creatPage("rankData");


//a连接转换事件
$("a").on("click",function(e){
  //e.preventDefault();
  if($(this).hasClass("my-prize")){
    window.Interface.toMyPrize();
  }else{
    window.location.href= $(this).attr("rel");
  }
});

});

function creatPage(){

  $(".settab .arrow-r").on("click",function(){
    $(this).parents(".settab").addClass("left");
  });
  $(".settab .arrow-l").on("click",function(){
    $(this).parents(".settab").removeClass("left");
  });

   // var r = D;
   setAllData("healthy","N");
   var ball = "healthy";
   window.Interface.tracking('排行榜', '健康星球', '');
   
  $(".settab li").on("click",function(){

    $(this).addClass("current").siblings("li").removeClass("current");
    ball = $(this).find("span").data("ball");

    //setAllData(ball,"N");
    if($("#rank-c-swi").hasClass("last")){
    setAllData(ball,"Y");
    }else{
    setAllData(ball,"N");
    }

switch (ball)
  {
  case "fun":
    window.Interface.tracking('排行榜', '娱乐星球', '')
    break;
  case "child":
    window.Interface.tracking('排行榜', '育儿星球', '')
    break;
  case "game":
    window.Interface.tracking('排行榜', '游戏星球', '')
    break;
  case "finance":
    window.Interface.tracking('排行榜', '理财星球', '')
    break;
  case "life":
    window.Interface.tracking('排行榜', '生活星球', '')
    break;
  case "healthy":
    window.Interface.tracking('排行榜', '健康星球', '')
    break;
  }



  })
  $("#rank-c-swi").off("click.a").on("click.a",function(){
    if($(this).hasClass("last")){
      $(this).removeClass("last");
      $("#rank-c-swi").text("上周排行榜");
      setAllData(ball,"N");
    }else{
      $(this).addClass("last");
      $("#rank-c-swi").text("本周排行榜");
      setAllData(ball,"Y");
      $("#rank-section").addClass("show-swi");
    }
  });

}

function setAllData(ball,lw){
      window.Interface.rankData({
      starType:ball,
      isLastWeek:lw
    },function(res){
      //生成列表
      creatRankList("#rank-list",res.rankingList);
      //判断擂主
      if(res.challenger && res.challenger.KEY && lw==="N"){
        $("#rank-challenger-bg").show();
        $("#rank-section").removeClass("no-challenger");
        $("#lastchampionname").text(res.challenger.nickName);
        $("#lastchampionscore").text(res.challenger.SCORE);
        if(res.challenger.headImgUrl){
          $("#lastchampionimg").attr("src",res.challenger.headImgUrl);
        }else{
          $("#lastchampionimg").attr("src","images/rank/head.png");
        }
        
      }else{
        $("#rank-challenger-bg").hide();
        $("#rank-section").addClass("no-challenger");
      }
        // "KEY":"",//手机号
        // "nickName":"",//昵称
        // "INDEX":"",//排名
        // "SCORE":"",//分数
        // "headImgUrl":""//擂主头像

      //个人信息
      $("#myrank").text(res.myRanking.INDEX);
      $("#myname").text(res.myRanking.nickName);
      $("#myscore").text(res.myRanking.SCORE);

      if(res.curIsChallengerNum){
        showPop("厉害了我的哥，你已经获取了3次擂主了，给别人留点机会吧！","确定","");
      }
      if(res.challenger && res.challenger.KEY){
        $("#rank-section").addClass("show-swi");
      }

      //判断是否显示成为擂主提示框
      var pop = localStorage.getItem(res.myRanking.KEY+res.week+ball);
      if(res.challengerIsMy  &&  !pop  && lw !== "Y"){
        localStorage.setItem(res.myRanking.KEY+res.week+ball,true);

        $(".pop-box-challenger").attr("class","pop-box-challenger").addClass("pop-"+ball).show();
        $(".pop-box-challenger .close").on("click",function(){
          $(".pop-box-challenger").fadeOut();
        });
        
        $(".pop-box-challenger .btn-share").off("click.a").on("click.a",function(){
          window.Interface.shareUrl({
            //starType:start
          },function(res){
            //window.location.href = res.url;
          });
          window.Interface.tracking('排行榜', '周擂主分战绩','');
        });
        $(".pop-box-challenger .btn-award").off("click.a").on("click.a",function(){
          window.Interface.toMyPrize();
          window.Interface.tracking('排行榜', '周擂主点击领奖','');
        });
      }
    });
}



function creatRankList(obj,data){
  var $obj = $(obj);
  $obj.empty();
  $(data).each(function(n,v){
    $obj.append('<li><div class="rank-box"><i>'+v.px+'</i><span>'+v.name+'</span><div class="score"><b>'+v.score+'</b>分</div></div></li>');
  });

}
